package com.zzty.txj1.controller;

import com.zzty.txj1.entity.*;
import com.zzty.txj1.service.QuestionService;
import com.zzty.txj1.service.SelfExamService;
import com.zzty.txj1.service.SelfExamTransactionService;
import com.zzty.txj1.util.ReadFileUtil;
import com.zzty.txj1.util.SplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/myCode")
public class MyCodeController {
    @Autowired
    private SelfExamTransactionService selfExamTransactionService;
    @Autowired
    private SelfExamService selfExamService;
    @Autowired
    private QuestionService questionService;
    @RequestMapping(value = "/initMyCode")
    public List<MyCodePage> initMyCode(User user) {
        System.out.println(user.toString());
        List<MyCodePage> myCodePages=new ArrayList<MyCodePage>();
        SplitUtil splitUtil = new SplitUtil();
        List<SelfExamTransaction> selfExamTransactions = selfExamTransactionService.getSelfExamTransaction(user);
        for(SelfExamTransaction selfExamTransaction:selfExamTransactions){
            StringBuilder sb=new StringBuilder();
            SelfExam selfExam=selfExamService.getSelfExamById(selfExamTransaction);
            List<Integer> ss=splitUtil.getSplit(selfExam.getQuestionId());
            for(Integer s:ss){
                Question question=new Question();
                question.setId(s);
                Question question1=questionService.getQuestionById(question);
                System.out.println("question1 is "+question1);
                sb.append(question1.getContent()+"；\n");
            }
            MyCodePage myCodePage=new MyCodePage();
            myCodePage.setDate(selfExam.getDate());
            myCodePage.setContent(String.valueOf(sb));
            myCodePage.setExamId(selfExam.getId());
            myCodePage.setUrl(selfExamTransaction.getAnswerUrl());
            System.out.println("myCodePage is"+myCodePage.toString());
            myCodePages.add(myCodePage);
        }
        return myCodePages;
    }
    @RequestMapping(value = "/getAnswer")
    public List<AnswerPage> getAnswer(MyCodePage myCodePage){
        System.out.println("url is "+myCodePage.getUrl());
        File file = new File(myCodePage.getUrl());
        String s[] = file.list();
        List<AnswerPage> answerPages = new ArrayList<AnswerPage>();
        for (String temp : s) {
            if(temp!=null){
                AnswerPage answerPage = new AnswerPage();
                ReadFileUtil readFileUtil = new ReadFileUtil();
                StringBuilder answer = readFileUtil.readJava(myCodePage.getUrl() +"/"+ temp);
                answerPage.setAnswer(answer);
                answerPage.setName(temp);
                answerPages.add(answerPage);
            }
        }
        return answerPages;
    }

    @RequestMapping(value = "/getSession",method = RequestMethod.POST)
    public User getNameBySession(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        return user;
    }
}
